# import requests
# # 导入漂亮制表
# from prettytable import PrettyTable
# # 导入json
# import json
# import os

# # 获取当前工作目录
# current_dir = os.getcwd()
# print(current_dir)

# with open('city.json', 'r', encoding='utf-8') as f:
#         city_data = json.load(f)

# from_city = input('请输入出发城市：')
# to_city = input('请输入目的城市：')
# date = '2024-12-10'

# table = PrettyTable()
# table.field_names = [
#     '序号', 
#     '车次', 
#     '出发时间',
#     '到达时间', 
#     '历时',
#     '特等座', 
#     '一等座', 
#     '二等座',
#     '硬卧', 
#     '硬座', 
#     '无座',
#     '软卧'
#     ]  
# page = 1
# url = f"https://kyfw.12306.cn/otn/leftTicket/queryO?leftTicketDTO.train_date={date}&leftTicketDTO.from_station={city_data[from_city]}&leftTicketDTO.to_station={city_data[to_city]}&purpose_codes=ADULT"
# headers = {
#     'cookie':'_uab_collina=173329185505964242771998; JSESSIONID=D312ACD738339C184DED1A96BE5B85D7; BIGipServerpassport=820510986.50215.0000; guidesStatus=off; highContrastMode=defaltMode; cursorStatus=off; route=9036359bb8a8a461c164a04f8f50b252; _jc_save_fromStation=%u5317%u4EAC%2CBJP; _jc_save_toStation=%u4E0A%u6D77%2CSHH; _jc_save_fromDate=2024-12-04; _jc_save_toDate=2024-12-04; _jc_save_wfdc_flag=dc; BIGipServerotn=1658388746.24610.0000',
#     'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36'
# }
# response = requests.get(url=url, headers=headers)
# response.encoding = 'utf-8'
# json_data = response.json()
# # 提取车次信息所在列表
# result = json_data['data']['result']
# # for循环遍历，提取列表元素
# for i in  result:
#     # 字符串分割，变成列表
#     index = i.split('|')
#     num = index[3] # 车次
#     start_time = index[8] # 出发时间
#     end_time = index[9] # 到达时间
#     use_time = index[10] # 历时
#     topGrade=index[32] # 特等座
#     first_class=index[31] # 一等座
#     second_class=index[30] # 二等座
#     hard_sleeper = index[28] #硬卧
#     hard_seat = index[29] # 硬座
#     no_seat = index[26] # 无座
#     soft_sleeper = index[23] # 软卧
#     dit = {
#         '车次':num,
#         '出发时间':start_time,
#         '到达时间':end_time,
#         '历时':use_time,
#         '特等座':topGrade,
#         '一等座':first_class,
#         '二等座':second_class,
#         '硬卧':hard_sleeper,
#         '硬座':hard_seat,
#         '无座':no_seat,
#        '软卧':soft_sleeper
#     }
#     table.add_row([
#         page,
#         num,
#         start_time,
#         end_time,
#         use_time,
#         topGrade,
#         first_class,
#         second_class,
#         hard_sleeper,
#         hard_seat,
#         no_seat,
#         soft_sleeper
#         ])
#     page += 1
# print(table)


"""""""""""""自动购票"""""""""""""
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome()
# 最大化窗口
driver.maximize_window()
url = 'https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc'
# 访问网站
driver.get(url)
# 输入出发地
driver.find_element_by_id('fromStationText').click()
driver.find_element_by_id('fromStationText').clear()
driver.find_element_by_id('fromStationText').send_keys('北京')
driver.find_element_by_id('fromStationText').send_keys(Keys.ENTER)


# 目的地
driver.find_element_by_id('toStationText').click()
driver.find_element_by_id('toStationText').clear()
driver.find_element_by_id('toStationText').send_keys('长沙')
driver.find_element_by_id('toStationText').send_keys(Keys.ENTER)
# 出发时间
driver.find_element_by_id('train_date').click()
driver.find_element_by_id('train_date').clear()
driver.find_element_by_id('train_date').send_keys('2024-12-10')
driver.find_element_by_id('train_date').send_keys(Keys.ENTER)

# 点击查询
driver.find_element_by_id('query_ticket').click()
time.sleep(1)

# 点击预订
driver.find_element_by_css_selector()

# 输入账号
driver.find_element_by_id('J-userName').send_keys('zhanghao')
# 输入密码
driver.find_element_by_id('J-password').send_keys('zhanghao')

# 点击登录
driver.find_element_by_id('J-login').click()

time.sleep(1)

# 输入身份证
driver.find_element_by_id('J-idCard').send_keys('8430')
# 点击验证码
driver.find_element_by_id('verification_code').click()
# 输入验证码
yzm = input('请输入验证码：')
driver.find_element_by_id('code').send_keys(yzm)

# 点击确定
driver.find_element_by_id('sureClick').click()
#
driver.implicitly_wait(10)

# 选择乘车人
driver.find_element_by_id('normalPassenger_0').click()    
# 提交订单
driver.find_element_by_id('submitOrder_id').click()
# 点击确定
driver.find_element_by_id('qr_submit_id').click()